<?php

/**
 * First test of the Mapper with getAll
 */

if (!defined('TESTS')) {
    header("HTTP/1.0 404 Not Found");
    die();
}

function identify ( $o )
{
    if (is_array($o)) {
        echo "\narray(";
        foreach ($o as $key => $oo) {
            echo "\n".$key." => ";
            identify($oo);
        }
        echo "\n)\n";
    } else
    echo is_object($o) ? $o : gettype($o);
}

class MySQLiConfig
{
    public $host = 'localhost';
    public $user = 'root';
    public $passwd = '';
    public $dbname = 'betauni';
    
    public $praefix = '';//'ugml_';
    
    public function __construct(MySQLiConnection $c)
    {} 
}

DB::getInstance()->mysqli = new MySQLiConnection();
MapperRegistry::set('Module', $m = new Service(new AutoUpdater('testmods'), new OwnedFactory(new MonoFactory('PhlameModule'), 'MODULE'))) ;
MapperRegistry::set('Phlame', 
    $s = new Service(
            new AutoUpdater('test'),
            new ParentFactory(
                new MonoFactory('TestPhlame'), 
                $m)
    )
);
$tests = $s->selectAll()->getAll();
identify($tests);
#var_dump($tests[1]);
foreach ($tests as $test) {
    #$test->test = substr($test->test2, 3, 4);
    echo $test->getID().'#';
    #$test->close();
    try {
        $test = NULL;
    } catch (DestroyException $e) {}
}
#new TestPhlame(array('id' => 'blubb', 'test' => '99999999999', 'test2' => ''));
#MapperRegistry::$_memory['Module'] = NULL;

#var_dump($GLOBALS);
#MapperRegistry::$_memory['Phlame'] = NULL;
#Cleaner::cleanUp();
#var_dump(MapperRegistry::$_memory);
#MapperRegistry::cleanUp();

echo '
'.(microtime(true) - $start).'
';


/*$res = DB::getInstance()->getPrimary()
->query('SELECT * FROM test');
print_r($res->fetchAll());

    [0] => Array
        (
            [id] => 1
            [test] => blubb
        )

    [1] => Array
        (
            [id] => 2
            [test] => blaa
        )

    [2] => Array
        (
            [id] => 3
            [test] => blubb
        )

    [3] => Array
        (
            [id] => 4
            [test] => blaa
        )


class Logger implements Mapper, Factory
{
    protected $_Object;
    public function __construct($Object) #ggf pr�fen ob es ein Objekt eines bestimmten Typs ist (per Typehint)
    {
        $this->_Object = $Object;
    }
    public function __call($method, $args = array())
    {
         printf('
Methode %s eines Objekts der Klasse %s wurde aufgerufen ', $method, get_class($this->_Object));
         # Hier kann man nat�rlich je nach Bedarf etwas Anderes einbauen... (e.g. In Datei schreiben etc.etc..)
         return call_user_func_array(array($this->_Object, $method), $args);
    }
    public function get(&$id = NULL)
    {
        return self::__call('get', array($id));
    }
    public function getAll()
    {
        return self::__call('getAll');
    }
    public function inform(array $data)
    {
        return self::__call('inform', array($data));
    }
    public function create(array $data)
    {
        return self::__call('create', array($data));
    }
}
/*'CREATE TABLE `test` (
	`id` BIGINT(11) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
	`test` VARCHAR(64) NOT NULL 
	);');*/


//*/

/*
$ar1 = array(
    354,
    3412,
    'safr',
    '43f',
    34.6    
);
$ar2 = array(
    364,
    'a' => 3412,
    'aafr',
    '43f',
    34.6    
);

$start = microtime(true);

for ($i=1;$i<=10000;++$i) {
    $r = array_intersect($ar1,$ar2);
}
echo microtime(true)-$start.' :: ';
print_r($r);

class A
{
        
    static public $Instance = NULL;
    static public function getInstance()
    {
        if (self::$Instance===NULL) {
            self::$Instance = new self();
        }
        return self::$Instance;
    }
    
    protected function __construct()
    {}
        
}


class B extends A
{
    static public function getInstance()
    {
        if (self::$Instance===NULL) {
            self::$Instance = new self();
        }
        return self::$Instance;
    }
    
    protected function __construct()
    {}
}
$a = A::getInstance();
$b = B::getInstance();
var_dump(A::$Instance);
var_dump(B::$Instance);

class A
{
    public function __destruct()
    {
        echo 'Destroyed:'.get_class($this).'
';
    }
}
class B extends A {}
class C extends A {}
class D extends A {}

$a = new A();
$b = new B();
$c = new C();
$d = new D();
$a->b = $b;
$a->c = $c;
//*/